/**
 * Created by CSJ11 on 2017/7/3.
 */
'use strict';
angular.module('app')
    .controller('frequencyController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '广告频率设置';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $scope.searchParams.adType=$("#adType").val();
                $.ajax({
                    url : $scope.app.project_name+'/frequency/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {

                        $scope.pageInfo = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }



            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $scope.searchParams.adType=$("#adType").val();
                $.ajax({
                    url : $scope.app.project_name+'/frequency/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                     console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                        $(".editSortBtn").show();
                        $(".saveSortBtn").hide();
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();


            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };


            $scope.businessLicenses = [];
            $scope.preview = function(businessLicense){
                if (businessLicense != null && businessLicense != '') {
                    $scope.businessLicenses = businessLicense.split('|');
                }
                $scope.firstApply = true;
            }

            $scope.oper = function (id,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/frequency/read/deleteById',
                    data: {id:id,isEnable:$event.target.checked?'Y':'N',state:$event.target.checked?'N':'Y'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }
            // 删除轮播图
            $scope.deleteById=function (id) {
                $scope.alertModel({
                    text:'确定删除？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name+'/frequency/read/deleteById',
                        data: {id:id}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);
                        }
                        $scope.search();
                    });

                })

            }

            // 编辑排序
            $scope.editSort=function (id,adType) {

                document.getElementById("clickPrice"+id).style.display="none";
                document.getElementById("btn"+id).style.display="inline";
                document.getElementById("clickPrices"+id).style.display="none";
                document.getElementById("btns"+id).style.display="inline";
                document.getElementById("editStart"+id).style.display="none";
                document.getElementById("editEnd"+id).style.display="inline";

            }

            $scope.edit=function (id,minNum,maxNum,adType) {
                var minNum=$("#minNum"+id).val();
                var maxNum=$("#maxNum"+id).val();
                if(isNaN(minNum)||minNum<=0||!(/^\d+$/.test(minNum))){
                        $scope.alertModal({'content':'只能输入整数'});
                        return ;
                    }
                if(isNaN(maxNum)||maxNum<=0||!(/^\d+$/.test(maxNum))){
                        $scope.alertModal({'content':'只能输入整数'});
                        return ;
                    }
                if(parseInt(minNum)< 0){
                    $scope.alertModal({'content':'最小值必须大于0'});
                    return;
                }
                if(parseInt(maxNum) < parseInt(minNum)|| parseInt(maxNum)== parseInt(minNum)){
                    $scope.alertModal({'content':'数据不正确，最小值大于了最大值'});
                    return;
                }

                saveData(id,minNum,maxNum,adType);
            }

            function saveData(id,minNum,maxNum,adType){
                    $.ajax({
                        url : $scope.app.project_name+'/frequency/read/update' ,
                        data: {"id":id,"minNum":minNum,"maxNum":maxNum,"adType":adType}
                    }).then(callback);
                function callback(result){
                    console.log(result);
                    if(result.record.resCode==10006 || result.record.resCode==10007 ) {
                        $scope.alertModal({"content": result.record.resMsg});

                    } else{
                        $scope.alertModal({"content":"操作成功"});

                        $timeout(function(){
                            $scope.search();
                        },2000);
                    }


                    $scope.loading = false;
                }
            }

        } ]);